package com.hwadee.ems.service.impl;

import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;

import java.time.LocalDate;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hwadee.ems.domain.dto.PageQuery;
import com.hwadee.ems.domain.dto.Result;
import com.hwadee.ems.domain.po.Approval;
import com.hwadee.ems.domain.po.ClassroomApplication;
import com.hwadee.ems.domain.vo.ClassroomApplicationVO;
import com.hwadee.ems.enums.ApprovalStatus;
import com.hwadee.ems.enums.ApprovalType;
import com.hwadee.ems.mapper.ClassroomApplicationMapper;
import com.hwadee.ems.service.IApprovalService;
import com.hwadee.ems.service.IClassroomApplicationService;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * <p>
 * 教室借用申请 服务实现类
 * </p>
 *
 * @author EnosElinsa
 * @since 2024/06/21
 */
@Service
public class ClassroomApplicationServiceImpl extends ServiceImpl<ClassroomApplicationMapper, ClassroomApplication> implements IClassroomApplicationService {

    private static IClassroomApplicationService instance;

    public static IClassroomApplicationService getInstance() {
        return instance;
    }

    @PostConstruct
    void init() {
        instance = this;
    }

    @Resource
    private IApprovalService approvalService;

    @Override
    public Result<IPage<ClassroomApplicationVO>> page(
        PageQuery pageQuery, 
        String applicantName, 
        String approverName,
        Integer approvalStatus,
        Long semesterId,
        Integer week
    ) {
        List<ClassroomApplicationVO> vos = baseMapper.selectPageByConditions(
            null,
            applicantName, 
            approverName, 
            approvalStatus, 
            semesterId, 
            week, 
            pageQuery.getSize(),
            (pageQuery.getCurrent() - 1) * pageQuery.getSize()
        );

        IPage<ClassroomApplicationVO> page = Page.of(pageQuery.getCurrent(), pageQuery.getSize());
        page.setTotal(count());
        page.setRecords(vos);

        return Result.ok(page);
    }

    @Override
    public Result<IPage<ClassroomApplicationVO>> pageByApplicantId(
        Long applicantId, 
        PageQuery pageQuery,        
        String approverName,
        Integer approvalStatus,
        Long semesterId,
        Integer week
    ) {
        List<ClassroomApplicationVO> vos = baseMapper.selectPageByConditions(
            applicantId, 
            null, 
            approverName, 
            approvalStatus, 
            semesterId, 
            week, 
            pageQuery.getSize(),
            (pageQuery.getCurrent() - 1) * pageQuery.getSize()
        );

        IPage<ClassroomApplicationVO> page = Page.of(pageQuery.getCurrent(), pageQuery.getSize());
        page.setTotal(count());
        page.setRecords(vos);

        return Result.ok(page);
    }

    @Override
    @Transactional
    public Result<Long> saveWithApproval(ClassroomApplication classroomApplication) {
        Approval approval = Approval.builder()
            .type(ApprovalType.CLASSROOM_USE)
            .status(ApprovalStatus.PENDING)
            .build();
        approvalService.save(approval);
        classroomApplication.setApprovalId(approval.getId());
        classroomApplication.setApplicationDate(LocalDate.now());
        if (save(classroomApplication)) {
            return Result.ok(classroomApplication.getId());
        } else {
            return Result.error();
        }
    }

}